/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBean;

import bean.business.curriculumBean;
import entities.Curriculum;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author HoangHai
 */
@ManagedBean
@RequestScoped
public class manageCurriculum {

    @EJB
    private curriculumBean curriculumBean;
    private List<Curriculum> list;
    private Curriculum cur = new Curriculum();
    private Curriculum cur1 = new Curriculum();

    public Curriculum getCur() {
        return cur;
    }

    public void setCur(Curriculum cur) {
        this.cur = cur;
    }
    
    public Curriculum getCur1() {
        return cur1;
    }

    public void setCur1(Curriculum cur1) {
        this.cur1 = cur1;
    }

    public List<Curriculum> getList() {
        list = curriculumBean.allCurriculum();
        return list;
    }

    public void setList(List<Curriculum> list) {
        this.list = list;
    }

    /** Creates a new instance of manageCurriculum */
    public manageCurriculum() {
    }

    public void insert() {
        if (checkName()) {
            curriculumBean.addCurriculum(cur1);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Information", "Sucessful"));
        }
    }

    public void update() {
        curriculumBean.updateCurriculum(cur);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Information", "Sucessful"));
    }

    public void delete() {
        curriculumBean.delCurriculum(cur.getCuID());
    }
    public boolean checkName(){
        List<Curriculum> curriculums = new LinkedList<Curriculum>();
        curriculums = curriculumBean.allCurriculum();
        for (Curriculum curriculum : curriculums) {
            if (curriculum.getCuName().equals(cur1.getCuName())) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Name had been Already"));
                return false;
            }
        }
        return true;
    }
}
